package ru.spbu.math.seabattle.client.jms.gui;

import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import ru.spbu.math.seabattle.client.jms.SeaBattleClient;
import ru.spbu.math.seabattle.entity.PlayerID;

public class GameRequestedGUI extends JFrame {

	private static final long serialVersionUID = 1L;
	
	private static final Rectangle WINDOW_RECTANGLE = new Rectangle(250, 200, 300, 100);
	private static final Rectangle LABEL_RECTANGLE = new Rectangle(10, 10, 280, 20);
	private static final Rectangle LBUTTON_RECTANGLE = new Rectangle(10, 40, 130, 20);
	private static final Rectangle RBUTTON_RECTANGLE = new Rectangle(150, 40, 130, 20);
	
	public GameRequestedGUI(final SeaBattleClient client, String opponentName, final PlayerID opponentID, int x, int y) {
	
		super("Game Request");
		
		this.getContentPane().setLayout(null);			
		this.setBounds(WINDOW_RECTANGLE);
		this.setVisible(true);
		this.setResizable(false);
		
		WINDOW_RECTANGLE.x = x;
		WINDOW_RECTANGLE.y = y;
		
		JLabel label = new JLabel(opponentName + " wants to play with you");
		JButton accept = new JButton("Accept");
		JButton decline = new JButton("Decline");
		
		label.setBounds(LABEL_RECTANGLE);
		accept.setBounds(LBUTTON_RECTANGLE);
		decline.setBounds(RBUTTON_RECTANGLE);
		
		this.add(label);
		this.add(accept);
		this.add(decline);
		
		label.setVisible(true);
		accept.setVisible(true);
		decline.setVisible(true);
		
		this.addWindowListener(
				new WindowListener() {
					@Override public void windowActivated(WindowEvent arg0) {}
					@Override public void windowClosed(WindowEvent e) {}
					@Override public void windowClosing(WindowEvent e) {
						client.onGameRequestDeclined(opponentID);							
					}
					@Override public void windowDeactivated(WindowEvent e) {}
					@Override public void windowDeiconified(WindowEvent e) {}
					@Override public void windowIconified(WindowEvent e) {}
					@Override public void windowOpened(WindowEvent e) {}				
				}
			);
		
		accept.addMouseListener(new MouseListener() {
			
			@Override
			public void mouseReleased(MouseEvent e) {}
			@Override public void mousePressed(MouseEvent e) {}
			@Override public void mouseExited(MouseEvent e) {}
			@Override public void mouseEntered(MouseEvent e) {}
			
			@Override 
			public void mouseClicked(MouseEvent e) {
				client.onGameRequestAccepted(opponentID);
			}
		});
		
		decline.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent e) {
				client.onGameRequestDeclined(opponentID);
			}
			@Override public void mouseEntered(MouseEvent e) {}
			@Override public void mouseExited(MouseEvent e) {}
			@Override public void mousePressed(MouseEvent e) {}
			@Override public void mouseReleased(MouseEvent e) {}
		});
	}
}
